﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Pseudogenerator.Common.Generators;

namespace Pseudogenerator.Common.ViewModels
{
    public class MixedCongruentialViewModel
        : CongruentialViewModel<MixedCongruential>
    {

        public MixedCongruentialViewModel()
        {
            this.ModeloActual = new MixedCongruential();
        }

        #region ModelFields

        public uint c
        {
            get
            {
                return ((MixedCongruential)ModeloActual).c;
            }
            set
            {
                if (((MixedCongruential)ModeloActual).c == value)
                    return;
                ((MixedCongruential)ModeloActual).c = value;
                OnPropertyChanged();
                RefreshGUI();
            }
        }

        #endregion

        public override string Title
        {
            get { return "Congruencial mixto"; }
        }

        public override bool CanRun
        {
            get
            {
                return ModeloActual.c > 0 &&
                       ModeloActual.m > ModeloActual.c
                       && base.CanRun;
            }
        }

        protected override void AddPreload()
        {
            var actual = Preloads.FirstOrDefault(i => i.a == a && i.x0 == x0 && i.c == c && i.m == m);
            if (actual == null)
            {
                Preloads.Add(new MixedCongruential() { a = a, x0 = x0, c = c, m = m });
            }
        }

        public override IEnumerable<string> Errors
        {
            get
            {
                if (ModeloActual.a <= 0 ||
                    ModeloActual.c <= 0)
                    yield return "Los parametros deben ser mayor que 0";
                if (ModeloActual.m <= ModeloActual.x0 ||
                    ModeloActual.m <= ModeloActual.a ||
                    ModeloActual.m <= ModeloActual.c)
                    yield return "El Modulo ('m') debe ser mayor que los demas parametros";
            }
        }

        public override IEnumerable<string> Recommendations
        {
            get
            {
                if (ModeloActual.a == 0 || ModeloActual.a == 1)
                    yield return "El valor de a = " + ModeloActual.a + " produce una serie no aleatoria.";

                if (ModeloActual.a < ((MixedCongruential)ModeloActual).c)
                    yield return "Se recomienda que 'a' sea mayor que 'c'.";
                if (ModeloActual.a % 2 == 0)
                    yield return "Se recomienda que 'a' sea impar.";
                if (ModeloActual.a % 3 == 0 || ModeloActual.a % 5 == 0)
                    yield return "Se recomienda que 'a' no sea divisible por 3 o 5.";
                if (((MixedCongruential)ModeloActual).c % 8 != 5)
                    yield return "Se recomienda que C mod 8 sea igual a 5.";

                #region comprobacion de numero primo del modulo

                int cont = 0;

                for (int i = 1; i <= ModeloActual.m; i++)
                {
                    if (ModeloActual.m % i == 0)
                        cont++;
                }

                if (cont > 2)
                    yield return "Se recomienda que m sea un numero primo.";

                #endregion

                #region comprobacion de arquitectura x64 o x86

                double p;
                //if (Environment.Is64BitOperatingSystem)
                //    p = Math.Pow(2, 64);
                //else
                    p = Math.Pow(2, 32);

                #endregion

                if (ModeloActual.m > p)
                    yield return "Se recomienda que m sea menor que " + p + ".";
                    
            }
        }

        private MixedCongruential _ModeloActual = null;
        public override MixedCongruential ModeloActual
        {
            get
            {
                if (_ModeloActual == null)
                    _ModeloActual = new MixedCongruential();

                return _ModeloActual;
            }
            set
            {
                if (value == null) 
                    return;
                this.a = value.a;
                this.x0 = value.x0;
                this.c = value.c;
                this.m = value.m;
                ClearCommand.Execute(null);
            }
        }
    }
}
